package ru.pb.auth.data.xml.parsers;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import ru.pb.auth.data.xml.holders.GameServersHolder;
import ru.pb.auth.model.GameServerInfo;
import ru.pb.global.xml.parsers.AbstractFileParser;

/**
 * @author: Felixx
 */
public class GameServersParser extends AbstractFileParser<GameServersHolder>
{
	private static GameServersParser _instance;

	public static GameServersParser getInstance()
	{
		if(_instance == null)
			_instance = new GameServersParser();
		return _instance;
	}

	private GameServersParser()
	{
		super("./data/gameservers.xml", GameServersHolder.getInstance());
	}

	@Override
	protected void readData(Node s)
	{
		if("list".equalsIgnoreCase(s.getNodeName()))
		{
			for(Node start = s.getFirstChild(); start != null; start = start.getNextSibling())
			{
				if("gameserver".equalsIgnoreCase(start.getNodeName()))
				{
					NamedNodeMap map = start.getAttributes();
					int id = Integer.parseInt(map.getNamedItem("id").getNodeValue());
					String passwd = map.getNamedItem("password").getNodeValue();
					String type = map.getNamedItem("type").getNodeValue();
					String ip = map.getNamedItem("ip").getNodeValue();

					getHolder().add(new GameServerInfo(id, passwd, type, ip));
				}
			}
		}
	}
}